package com.riche.codefactory.controller;

import com.riche.codefactory.common.BaseResponse;
import com.riche.codefactory.common.ErrorCode;
import com.riche.codefactory.common.R;
import com.riche.codefactory.exception.BusinessException;
import com.riche.codefactory.model.c2s.postthumb.C2sPostThumbAddRequest;
import com.riche.codefactory.model.domain.User;
import com.riche.codefactory.service.IPostThumbSVC;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.riche.codefactory.service.IUserSVC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 帖子点赞接口
 *
 *

 */
@RestController
@RequestMapping("/post_thumb")
@Slf4j
public class PostThumbController {

    @Resource
    private IPostThumbSVC IPostThumbSVC;

    @Resource
    private IUserSVC userService;

    /**
     * 点赞 / 取消点赞
     *
     * @param c2sPostThumbAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doThumb(@RequestBody C2sPostThumbAddRequest c2sPostThumbAddRequest,
            HttpServletRequest request) {
        if (c2sPostThumbAddRequest == null || c2sPostThumbAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能点赞
        final User loginUser = userService.getLoginUser(request);
        long postId = c2sPostThumbAddRequest.getPostId();
        int result = IPostThumbSVC.doPostThumb(postId, loginUser);
        return R.ok(result);
    }

}
